package com.health.mapp.impl;

import java.math.BigDecimal;

import org.apache.commons.lang3.StringUtils;


import com.health.db.model.UserAccountLedger;
import com.health.db.model.UserAccountVoucherBill;
import com.health.db.service.user.IUserAccountJournalService;
import com.health.db.service.user.IUserAccountLedgerService;
import com.health.db.service.user.IUserAccountVoucherBillService;
import com.health.mapp.bean.GetCashApplyReq;
import com.health.mapp.bean.GetCashApplyResp;
import com.health.mapp.bean.Header;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;

/**
 *  医护取现申请接口 --仅供医生查询
 * @author HiRoad
 *
 */
public class GetCashApplyImpl implements ActionInterface {
	
	private IUserAccountLedgerService userAccountLedgerService  ;
	private IUserAccountJournalService userAccountJournalService;
	private IUserAccountVoucherBillService userAccountVoucherBillService;
	
	@Override
	public Response execute(Header header, Object reqBean) throws Exception {
		
		GetCashApplyReq req=(GetCashApplyReq) reqBean;
		GetCashApplyResp _resp= new GetCashApplyResp();
		

		//flag=1为确认付款
		String flag = req.getFlag();
		
		//flag=2为取消付款
		if (StringUtils.isNotEmpty(flag)&&("2").equals(flag)) {
			Long docId = req.getDocId();
			String userId = req.getUserId();
			
			if (StringUtils.isEmpty(userId)) {
				_resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
				_resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
				return _resp;
			}
			int executNum= userAccountVoucherBillService.cancelAccountVoucherBill(docId,Long.valueOf(userId));
			
			//确认插入正确
			if (executNum!=1) {
				_resp.setErrorCode(ResultCode.OPERATION_OTHER_ERROR);
				_resp.setErrorMsg(ResultCode.OPERATION_OTHER_ERROR_NOTE);
				return _resp;
			}
			
		}else if (StringUtils.isNotEmpty(flag)&&("1").equals(flag)) {
			//确认付款
			Long docId = req.getDocId();
			String userId = req.getUserId();
			
			if (StringUtils.isEmpty(userId)) {
				_resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
				_resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
				return _resp;
			}
			
		
			
			int executNum= userAccountVoucherBillService.updateAccountVoucherBill(docId,Long.valueOf(userId));
			
			//确认插入正确
			if (executNum!=1) {
				_resp.setErrorCode(ResultCode.OPERATION_OTHER_ERROR);
				_resp.setErrorMsg(ResultCode.OPERATION_OTHER_ERROR_NOTE);
				return _resp;
			}
			
		}else{
			
			Long userId = req.getDocId();
			String cashAmountStr = req.getGetCashAmount();
			
			
			if (userId==0) {
				_resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
				_resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
				return _resp;
			}else{
				
				UserAccountLedger  userAccountLedger = userAccountLedgerService.selectUserAccountLedgerByUserId(userId);
				
				if (null== userAccountLedger||null==userAccountLedger.getUserCashAccountIdNo()) {
					_resp.setErrorCode(ResultCode.OPERATION_OTHER_ERROR);
					_resp.setErrorMsg("此用户无可提现的账户");
					return _resp;
				}
				BigDecimal temp ;
				
				if (StringUtils.isBlank(cashAmountStr)&&(userAccountLedger.getBalanceTotal().compareTo(BigDecimal.ZERO)>0)) {
					temp = userAccountLedger.getBalanceTotal();
				}else{
					
					try{
						BigDecimal amount=	new BigDecimal(cashAmountStr);
						
						if (userAccountLedger.getBalanceTotal().compareTo(amount)>0) {
							temp =amount;
						}else{
							temp = userAccountLedger.getBalanceTotal();
						}
					}catch (Exception e) {
						//传入不是数字型的报错
						_resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
						_resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
						return _resp;
					}
				}
				
				if (temp.compareTo(BigDecimal.ZERO)>0) {
					UserAccountVoucherBill userAccountVoucherBill = new UserAccountVoucherBill();
					userAccountVoucherBill.setRunFlag((short) 0);
					userAccountVoucherBill.setUserid(userAccountLedger.getAccountid());
					
					//查询是否有之前申请过的体现单
					if (userAccountVoucherBillService.countUserAccountVoucherBillSimple(userAccountVoucherBill)>0) {
						_resp.setErrorCode(ResultCode.OPERATION_OTHER_ERROR);
						_resp.setErrorMsg("有等待通过的提现单！不能重复提现！");
						return _resp;
					}
					
					userAccountVoucherBill.setBillTotal(temp);
					int executNum= userAccountVoucherBillService.insertUserAccountVoucherBill(userAccountLedger,userAccountVoucherBill);
					
					//确认插入正确
					if (executNum!=1) {
						_resp.setErrorCode(ResultCode.OPERATION_OTHER_ERROR);
						_resp.setErrorMsg(ResultCode.OPERATION_OTHER_ERROR_NOTE);
						return _resp;
					}
				}
			}
		}
		return _resp;
	}

	public IUserAccountLedgerService getUserAccountLedgerService() {
		return userAccountLedgerService;
	}

	public void setUserAccountLedgerService(
			IUserAccountLedgerService userAccountLedgerService) {
		this.userAccountLedgerService = userAccountLedgerService;
	}

	public IUserAccountJournalService getUserAccountJournalService() {
		return userAccountJournalService;
	}

	public void setUserAccountJournalService(
			IUserAccountJournalService userAccountJournalService) {
		this.userAccountJournalService = userAccountJournalService;
	}

	public IUserAccountVoucherBillService getUserAccountVoucherBillService() {
		return userAccountVoucherBillService;
	}

	public void setUserAccountVoucherBillService(
			IUserAccountVoucherBillService userAccountVoucherBillService) {
		this.userAccountVoucherBillService = userAccountVoucherBillService;
	}

	
	
}
